<?php


class QuestionService extends ServiceAbstract {
	
	private $_mapper;
	
	public function __construct(){
		$this->_mapper = new Db_Mapper_Question();
	}
	
	public function fetchQuestionsBySid(){
			
		$where = $this->_mapper->getTable()->select()->where('sid = ?',$this->getParam('sid'),Zend_Db::INT_TYPE); 
		$this->toXml($this->_mapper->fetchAll($where));
	}
	
	public function editQuestionById(){
		
		$qid = (int)$this->getParam('qid');
		$text = $this->getParam('text');	
		
		$updatedRows = $this->_mapper->getTable()->update(array(
			'text' => $text 
			),
			$this->_mapper->getTable()->getAdapter()->quoteInto('qid = ?',$qid,Zend_Db::INT_TYPE)
		);
		
		if($updatedRows > 0){
			echo 'true';
		}
		else {
			echo 'false';
		}		
	}
	
	public function deleteById(){
		$qid = (int)$this->getParam('qid');
				
		if( ($this->_mapper->delete(new Db_Model_Question(array('qid' => $qid)))) > 0){
			echo 'true';
		}else {
			echo 'false';
		}
	}
	
	public function addQuestion(){
		$sid = $this->getParam('sid');
		$qn = $this->getParam('qn');
		$text = $this->getParam('text');
		
		$data = array(
			'sid' => $sid,
			'qn' => $qn,
			'text' => $text
		);
		
		
		if(($qid = $this->_mapper->save(new Db_Model_Question($data))) > 0){
			echo $qid;
		}
		else {
			echo 'false';
		}
	}
}

?>